package com.example.lets;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class SearchResult extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search_result);

		Button back = (Button) this.findViewById(R.id.search_result_back);
		back.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				SearchResult.this.finish();
			}
		});

		Intent in = getIntent();
		String mode = in.getStringExtra("mode");
		String input = in.getStringExtra("input");
		if(mode.equals("type"))
			searchActiByType(input);
		else if(mode.equals("sponsored"))
			searchSponsoredUserActivity(input);
		else if(mode.equals("joined"))
			searchJoinedUserActivity(input);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.search_result, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	protected void searchActiByType(String type) {
		BmobQuery<UserActivity> ActivityQuery = new BmobQuery<UserActivity>("UserActivity");
		ActivityQuery.setLimit(100);
		ActivityQuery.addWhereEqualTo("type", type);
		ActivityQuery.findObjects(this, new FindListener<UserActivity>(){

			@Override
			public void onError(int arg0, String arg1) {
				Toast.makeText(SearchResult.this, "error :", Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onSuccess(List<UserActivity> ActivityQuery) {
				ArrayList<UserActivity> retAct = new ArrayList<UserActivity>();
				for (UserActivity userActivity : ActivityQuery) {
					// ????????��?????????????????????????????
					retAct.add(userActivity);
				}
				final ArrayList<HashMap<String, Object>> listActi = new ArrayList<HashMap<String, Object>>();

				// ??��????????????????????????listview??
				ListView actiSearched = (ListView) SearchResult.this.findViewById(R.id.search_event_view);
				actiSearched.setOnItemClickListener(new AdapterView.OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> parent, View view,
											int position, long id) {

						String title = (String) listActi.get(position).get("ItemTitle");
						String description = (String) listActi.get(position).get("ItemText");
						String picFileName = (String) listActi.get(position).get("ItemPicFileName");
						String sponsorId = (String) listActi.get(position).get("sponsorId");
						String type = (String) listActi.get(position).get("type");
						String place = (String) listActi.get(position).get("place");
						String time = (String) listActi.get(position).get("time");
						String ActiId = (String) listActi.get(position).get("id");

						Intent intent = new Intent(SearchResult.this, EventActivity.class);
						intent.putExtra("title", title);
						intent.putExtra("description", description);
						intent.putExtra("picFileName", picFileName);
						intent.putExtra("sponsorId", sponsorId);
						intent.putExtra("place", place);
						intent.putExtra("time", time);
						intent.putExtra("type", type);
						intent.putExtra("id", ActiId);

						startActivity(intent);
					}

				});


				for (UserActivity activity : retAct) {
					HashMap<String, Object> mapAct = new HashMap<String, Object>();
					mapAct.put("ItemSponsor", activity.getSponsorName());
					mapAct.put("ItemPicFileName", activity.getPicFilename());
					mapAct.put("ItemTitle", activity.getName());
					mapAct.put("number", activity.getPeopleNumber()); // TODO
					mapAct.put("sponsorId", activity.getSponsorId());
					mapAct.put("place", activity.getPlace());
					mapAct.put("time", activity.getStartTime().getDate());
					mapAct.put("type", activity.getType());
					mapAct.put("id", activity.getObjectId());
					listActi.add(mapAct);
				}
				actiSearched.setAdapter(new MyAdapter(SearchResult.this, listActi));

				Toast.makeText(SearchResult.this, "查询到了活动共:"+retAct.size(), Toast.LENGTH_SHORT).show();
			}
		});
	}


	private static ArrayList<HashMap<String, Object>> joinedList;
	private void searchJoinedUserActivity(final String inputId) {

		ListView invoSearched = (ListView) SearchResult.this.findViewById(R.id.search_event_view);
		joinedList = new ArrayList<HashMap<String, Object>>();
		invoSearched.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
									int position, long id) {

				String title = (String) joinedList.get(position).get("ItemTitle");
				String description = (String) joinedList.get(position).get("ItemText");
				String picFileName = (String) joinedList.get(position).get("ItemPicFileName");
				String sponsorId = (String) joinedList.get(position).get("sponsorId");
				String type = (String) joinedList.get(position).get("type");
				String place = (String) joinedList.get(position).get("place");
				String time = (String) joinedList.get(position).get("time");
				String ActiId = (String) joinedList.get(position).get("id");

				Intent intent = new Intent(SearchResult.this, EventActivity.class);
				intent.putExtra("title", title);
				intent.putExtra("description", description);
				intent.putExtra("picFileName", picFileName);
				intent.putExtra("sponsorId", sponsorId);
				intent.putExtra("place", place);
				intent.putExtra("time", time);
				intent.putExtra("type", type);
				intent.putExtra("id", ActiId);

				startActivity(intent);
			}

		});
		final MyAdapter mAdapter = new MyAdapter(SearchResult.this, joinedList);
		invoSearched.setAdapter(mAdapter);

		BmobQuery<Involvement> joinedInvoQuery = new BmobQuery<Involvement>("Involvement");
		joinedInvoQuery.addWhereEqualTo("userId", inputId);
		joinedInvoQuery.setLimit(100);
		joinedInvoQuery.findObjects(this, new FindListener<Involvement>() {
			@Override
			public void onSuccess(List<Involvement> joinedUserActivityResult) {
				// TODO Auto-generated method stub
				for (Involvement invo : joinedUserActivityResult) {
					String actId = invo.getActivityId();
					BmobQuery<UserActivity> joinedUserActivityQuery = new BmobQuery<UserActivity>("UserActivity");
					joinedUserActivityQuery.addWhereEqualTo("objectId", actId);
					joinedUserActivityQuery.findObjects(SearchResult.this, new FindListener<UserActivity>() {

						@Override
						public void onSuccess(List<UserActivity> list) {
							for (UserActivity activity : list) {
								HashMap<String, Object> mapAct = new HashMap<String, Object>();
								mapAct.put("ItemSponsor", activity.getSponsorName());
								mapAct.put("ItemPicFileName", activity.getPicFilename());
								mapAct.put("ItemTitle", activity.getName());
								mapAct.put("number", activity.getPeopleNumber()); // TODO
								mapAct.put("sponsorId", activity.getSponsorId());
								mapAct.put("place", activity.getPlace());
								mapAct.put("time", activity.getStartTime().getDate());
								mapAct.put("type", activity.getType());
								mapAct.put("id", activity.getObjectId());
								joinedList.add(mapAct);
							}
							mAdapter.notifyDataSetChanged();
						}

						@Override
						public void onError(int i, String s) {

						}
					});
				}
			}

			@Override
			public void onError(int arg0, String arg1) {
				// TODO Auto-generated method stub
				Toast.makeText(SearchResult.this, "error :", Toast.LENGTH_SHORT).show();
			}
		});
	}

	private void searchSponsoredUserActivity(final String inputId) {
		BmobQuery<UserActivity> sponsoredUserActivityQuery = new BmobQuery<UserActivity>("UserActivity");
		sponsoredUserActivityQuery.addWhereEqualTo("sponsorId", inputId);
		sponsoredUserActivityQuery.setLimit(100);
		sponsoredUserActivityQuery.findObjects(this, new FindListener<UserActivity>() {
			@Override
			public void onSuccess(List<UserActivity> sponsoredUserActivityResult) {
				// TODO Auto-generated method stub
				// ????????????JSONArray,??????��???
				Log.i("find", "????????????????");
				ArrayList<UserActivity> retAct = new ArrayList<UserActivity>();
				for (UserActivity userActivity : sponsoredUserActivityResult) {
					// ????????��?????????????????????????????
					retAct.add(userActivity);
				}
				final ArrayList<HashMap<String, Object>> listActi = new ArrayList<HashMap<String, Object>>();

				// ??��????????????????????????listview??
				ListView actiSearched = (ListView) SearchResult.this.findViewById(R.id.search_event_view);
				actiSearched.setOnItemClickListener(new AdapterView.OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> parent, View view,
											int position, long id) {

						String title = (String) listActi.get(position).get("ItemTitle");
						String description = (String) listActi.get(position).get("ItemText");
						String picFileName = (String) listActi.get(position).get("ItemPicFileName");
						String sponsorId = (String) listActi.get(position).get("sponsorId");
						String type = (String) listActi.get(position).get("type");
						String place = (String) listActi.get(position).get("place");
						String time = (String) listActi.get(position).get("time");
						String ActiId = (String) listActi.get(position).get("id");

						Intent intent = new Intent(SearchResult.this, EventActivity.class);
						intent.putExtra("title", title);
						intent.putExtra("description", description);
						intent.putExtra("picFileName", picFileName);
						intent.putExtra("sponsorId", sponsorId);
						intent.putExtra("place", place);
						intent.putExtra("time", time);
						intent.putExtra("type", type);
						intent.putExtra("id", ActiId);

						startActivity(intent);
					}

				});

				for (UserActivity activity : retAct) {
					HashMap<String, Object> mapAct = new HashMap<String, Object>();
					mapAct.put("ItemSponsor", activity.getSponsorName());
					mapAct.put("ItemPicFileName", activity.getPicFilename());
					mapAct.put("ItemTitle", activity.getName());
					mapAct.put("number", activity.getPeopleNumber()); // TODO
					mapAct.put("sponsorId", activity.getSponsorId());
					mapAct.put("place", activity.getPlace());
					mapAct.put("time", activity.getStartTime().getDate());
					mapAct.put("type", activity.getType());
					mapAct.put("id", activity.getObjectId());
					listActi.add(mapAct);
				}
				actiSearched.setAdapter(new MyAdapter(SearchResult.this, listActi));

				Toast.makeText(SearchResult.this, "查询到了活动共:" + retAct.size() + "个" , Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onError(int arg0, String arg1) {
				// TODO Auto-generated method stub
				Toast.makeText(SearchResult.this, "error :", Toast.LENGTH_SHORT).show();
			}
		});
	}

	@Override
	public void onBackPressed() {
		finish();
		return;
	}
}

